﻿using System;
using System.ComponentModel;
using Analyzer.ViewModel;
using Model;

namespace QualityProfileEditor.ViewModel
{
    internal class QualityProfileItemViewModel : ViewModelBase, IDataErrorInfo
    {
        #region Lifecycle

        public QualityProfileItemViewModel(QualityProfileItem model, ActualEntities context)
        {
            if (model == null)
                throw new ArgumentNullException("model");
            if (context == null)
                throw new ArgumentNullException("context");
            this.Model = model;
            this.Context = context;
            this.Model.PropertyChanged += new PropertyChangedEventHandler(Model_PropertyChanged);
        }

        #endregion Lifecycle

        #region Properties

        private QualityProfileItem Model { get; set; }

        private ActualEntities pContext;
        private ActualEntities Context
        {
            get { return pContext; }
            set
            {
                if (value != pContext)
                {
                    pContext = value;
                    OnPropertyChanged("CategoryName");
                    OnPropertyChanged("Weight");
                }
            }
        }

        public string CategoryName
        {
            get
            {
                return this.Model.CategoryName;
            }
        }

        private int pWeight;
        public int Weight
        {
            get
            {
                if (pWeight < 0)
                    return pWeight;
                else
                    return this.Model.Weight;
            }
            set
            {
                pWeight = value;
                if (value >= 0)
                {
                    this.Model.Weight = value;
                    this.Context.SaveChanges();
                }
                OnPropertyChanged("Weight");
            }
        }

        #endregion Properties

        #region EventHandlers

        void Model_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "Weight":
                    pWeight = this.Model.Weight;// Use of this.Weight causes StackOverflow
                    OnPropertyChanged("Weight");
                    return;
                default:
                    break;
            }
        }

        #endregion EventHandlers

        #region IDataErrorInfo Members

        public string Error
        {
            get { throw new NotImplementedException(); }
        }

        public string this[string columnName]
        {
            get
            {
                string msg = null;
                switch (columnName)
                {
                    case "Weight":
                        if (this.Weight < 0)
                            msg = "A súly nem lehet kisebb 0-nál!";
                        break;
                    default:
                        throw new ArgumentException(
                            "Unrecognized property: " + columnName);
                }
                return msg;
            }
        }

        #endregion IDataErrorInfo Members
    }
}
